package com.hydl.power.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.LoadMapper;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.mapper.TransformerMapper;
import com.hydl.power.pojo.Load;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.pojo.Transformer;
import com.hydl.power.service.LoadService;
import com.hydl.power.utils.TableUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.transform.Source;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wxb
 * @date 2019/12/28 10:37
 */
@Service
public class LoadServiceImpl implements LoadService {
    @Autowired
    private LoadMapper loadMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TransformerMapper transformerMapper;



    @Override
    public  TableUtils selectByConditions(Integer pageNum , Integer pageSize, String area,String project,String powerRoom,String dataType,String startTime,String endTime) {

        Page page = PageHelper.startPage(pageNum,pageSize);
        List<Load> list = loadMapper.selectByConditions(area,project,powerRoom,dataType,startTime,endTime);
        TableUtils tableUtils = new TableUtils();
        tableUtils.setTotal(page.getTotal());
        tableUtils.setRows(list);
        return tableUtils;
    }

    @Override
    public PageInfo<Load> queryLoad(Integer pageNum, Integer pageSize, String cityId, String projectId, String roomId, String token, String startTime, String endTime, String dataType) {
        List<Transformer> transformer=transformerMapper.selectByCondition(null,null,null,null,null);
        DecimalFormat df = new DecimalFormat("0.00%");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Load> list = new ArrayList<>();
        SysUser sysUser = sysUserMapper.getUserByToken(token);
       if(sysUser!=null){
           if(sysUser.getStatus() == 2){
               if(sysUser.getProjectId()==null){
                   return new PageInfo<>(list);
               }
               if(dataType=="hour" || dataType.equals("hour")){
                   list= loadMapper.queryLoadByhour(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),roomId,startTime,endTime);
               }else if(dataType=="day" || dataType.equals("day")){
                   list= loadMapper.queryLoadByDay(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),roomId,startTime,endTime);
               }else{
                   list= loadMapper.queryLoadByMonth(pageNum,pageSize,cityId,sysUser.getProjectId().toString(),roomId,startTime);
               }
               if(list!=null){
                   for (Load load : list) {
                       //load.setLoadRate(df.format(Double.parseDouble(load.getLoadRate())));
                     for (Transformer transformer1 : transformer) {
                       if(load.getDeviceName().equals(transformer1.getDeviceName())){
                         load.setLoadRate(df.format(Double.parseDouble(load.getLoadRate())/Double.parseDouble(transformer1.getTransformerCapacity())));
                         load.setTransformerName(transformer1.getTransformerName());
                       }
                     }
                   }
               }
               return  new PageInfo<>(list);
           }
       }
        if(dataType=="hour" || dataType.equals("hour")){
            list= loadMapper.queryLoadByhour(pageNum,pageSize,cityId,projectId,roomId,startTime,endTime);
        }else if(dataType=="day" || dataType.equals("day")){
            list= loadMapper.queryLoadByDay(pageNum,pageSize,cityId,projectId,roomId,startTime,endTime);
        }else{
            list= loadMapper.queryLoadByMonth(pageNum,pageSize,cityId,projectId,roomId,startTime);
        }
        if(list!=null){
            for (Load load : list) {
              //load.setLoadRate(df.format(Double.parseDouble(load.getLoadRate())));
              for (Transformer transformer1 : transformer) {
                if(load.getDeviceName().equals(transformer1.getDeviceName())){
                  load.setLoadRate(df.format(Double.parseDouble(load.getLoadRate())/Double.parseDouble(transformer1.getTransformerCapacity())));
                  load.setTransformerName(transformer1.getTransformerName());
                }
              }
            }
        }
        return  new PageInfo<>(list);
    }

  @Override
  public PageInfo<Load> queryLoads(Integer pageNum, Integer pageSize, String startTime, String deviceId) throws ParseException {
    Map map = new HashMap();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      List<Load> list = loadMapper.queryLoad(pageNum,pageSize,startTime,deviceId);
    System.out.println(list);
    List<Transformer> transformer=transformerMapper.selectByCondition(null,null,null,null,null);
    DecimalFormat df = new DecimalFormat("0.00%");
    if(list!=null){
      if(transformer!=null){
        for (Load load : list) {
        //load.setLoadRate(df.format(Double.parseDouble(load.getLoadRate())));
          for (Transformer transformer1 : transformer) {
            if(load.getDeviceName().equals(transformer1.getDeviceName())){
              Date dt2 =sdf.parse(transformer1.getInstallTime());
              Date dt1 =new Date();
              Calendar  from  =  Calendar.getInstance();
              from.setTime(dt2);
              Calendar  to  =  Calendar.getInstance();
              to.setTime(dt1);
              //只要年月
              int fromYear = from.get(Calendar.YEAR);
              int fromMonth = from.get(Calendar.MONTH);
              int toYear = to.get(Calendar.YEAR);
              int toMonth = to.get(Calendar.MONTH);
              int month = toYear *  12  + toMonth  -  (fromYear  *  12  +  fromMonth);
              load.setInstallTime(String.valueOf(month));
              load.setLoadRate(df.format(Double.parseDouble(load.getPtin())*1.732/Double.parseDouble(transformer1.getTransformerCapacity())));
              load.setTransformerCapacity(transformer1.getTransformerCapacity());
            }
          }
        }
      }else {
        list=new ArrayList<>();
        return  new PageInfo<>(list);
      }
    }
     return  new PageInfo<>(list);
  }
}
